package com.exercise.widgets.viewpager2

import android.view.LayoutInflater
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel
import com.exercise.widgets.databinding.ActivityCardFragmentBinding
import com.exercise.widgets.viewpager2.fragments.NestViewPagerFragment
import com.exercise.widgets.viewpager2.fragments.NormalFragment
import com.google.android.material.tabs.TabLayoutMediator

class CardFragmentActivity : BaseActivity<BaseViewModel, ActivityCardFragmentBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivityCardFragmentBinding
        get() = ActivityCardFragmentBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java
    private val mTitle: List<Pair<String, Fragment>> = listOf(
        "Item1" to NormalFragment.newInstance("Item1"),
        "Item2" to NormalFragment.newInstance("Item2"),
        "Item3" to NestViewPagerFragment.newInstance()
    )

    override fun initData() {
    }

    override fun initView() {
        binding.viewPager2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int {
                return mTitle.size
            }

            override fun createFragment(position: Int): Fragment {
                return mTitle[position].second
            }

        }
        TabLayoutMediator(binding.tabLayout, binding.viewPager2) { tab, position ->
            tab.text = mTitle[position].first
        }.attach()
    }

}